/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ufreedom.demo.slice;

import com.ufreedom.demo.ResourceTable;
import com.ufreedom.demo.UiUtils;
import com.ufreedom.demo.transition.BeerFloating;
import com.ufreedom.demo.transition.PlaneFloating;
import com.ufreedom.demo.transition.StarFloating;
import com.ufreedom.floatingview.Floating;
import com.ufreedom.floatingview.FloatingBuilder;
import com.ufreedom.floatingview.FloatingElement;
import com.ufreedom.floatingview.effect.ScaleFloatingTransition;
import com.ufreedom.floatingview.effect.TranslateFloatingTransition;
import com.ufreedom.floatingview.spring.ReboundListener;
import com.ufreedom.floatingview.spring.SpringHelper;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

/**
 * MainAbilitySlice
 *
 * @since 2021-03-31
 */
public class MainAbilitySlice extends AbilitySlice {
    private Floating mFloating;
    private Component mIcPlaneView;
    private Component mIcPaperPlane;
    private int mScreenHeight;
    private Component dependentLayout;
    private final int marginAll = 15;
    private final int two = 2;
    private final float heightUtil = 0.53f;
    private final double bounciness = 11;
    private final double speed = 15;
    private final int starOffsetX = -80;

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setStatusBarColor(Color.getIntColor("#6d5f88"));
        super.setUIContent(ResourceTable.Layout_ability_main);
        dependentLayout = findComponentById(ResourceTable.Id_dependent_layout);
        mFloating = new Floating(this, dependentLayout);
        mScreenHeight = UiUtils.getScreenWidth(this);
        initLayout();
    }

    /**
     * 初始化动画
     */
    private void initLayout() {
        // 底部大飞机
        setIcPlaneView();

        // 纸飞机
        setIcPaperAirPlaneView();

        // C代码
        setIcCommandLineView();

        // 点赞
        setIcLikeView();

        // 旋转星星
        setIcStarView();

        // 啤酒
        setIcBeerView();
    }

    /**
     * 底部飞机
     */
    private void setIcPlaneView() {
        mIcPlaneView = findComponentById(ResourceTable.Id_icPlane);
        mIcPlaneView.setClickedListener(view -> {
            Image iv = new Image(this);
            iv.setLayoutConfig(new ComponentContainer.LayoutConfig(mIcPlaneView.getWidth(), mIcPlaneView.getHeight()));
            iv.setPixelMap(ResourceTable.Media_floating_plane);
            FloatingElement floatingElement = new FloatingBuilder()
                    .anchorView(mIcPlaneView)
                    .targetView(iv)
                    .floatingTransition(new PlaneFloating(this))
                    .build();
            mFloating.startFloating(floatingElement);
        });
    }

    /**
     * 纸飞机
     */
    private void setIcPaperAirPlaneView() {
        mIcPaperPlane = findComponentById(ResourceTable.Id_icPaperAirPlane);
        mIcPaperPlane.setClickedListener(view -> {
            Image iv = new Image(this);
            iv.setLayoutConfig(new ComponentContainer.LayoutConfig(mIcPaperPlane.getWidth(),
                    mIcPaperPlane.getHeight()));
            iv.setPixelMap(ResourceTable.Media_paper_airplane);
            FloatingElement floatingElement = new FloatingBuilder()
                    .anchorView(mIcPaperPlane)
                    .targetView(iv)
                    .floatingTransition(new TranslateFloatingTransition())
                    .build();
            mFloating.startFloating(floatingElement);
        });
    }

    /**
     * C代码
     */
    private void setIcCommandLineView() {
        Component icCommandLineView = findComponentById(ResourceTable.Id_icCommandLine);
        icCommandLineView.setClickedListener(view -> {
            Text textView = new Text(this);
            textView.setText("Hello FloatingView");
            FloatingElement floatingElement = new FloatingBuilder()
                    .anchorView(view)
                    .targetView(textView)
                    .offsetY(-view.getHeight())
                    .offsetX(-view.getWidth())
                    .floatingTransition(new ScaleFloatingTransition())
                    .build();
            mFloating.startFloating(floatingElement);
        });
    }

    /**
     * 点赞
     */
    private void setIcLikeView() {
        Image icLikeView = (Image) findComponentById(ResourceTable.Id_icLike);
        icLikeView.setClickedListener(view -> {
            FloatingElement floatingElement = new FloatingBuilder()
                    .anchorView(icLikeView)
                    .targetView(ResourceTable.Layout_ic_like)
                    .offsetX(-icLikeView.getWidth())
                    .offsetY(-icLikeView.getHeight())
                    .floatingTransition(new TranslateFloatingTransition())
                    .build();
            mFloating.startFloating(floatingElement);
        });
    }

    /**
     * 旋转星星
     */
    private void setIcStarView() {
        final Component icStarView = findComponentById(ResourceTable.Id_icStar);
        icStarView.setClickedListener(view -> {
            Image iv = new Image(this);
            iv.setLayoutConfig(new ComponentContainer.LayoutConfig(icStarView.getWidth(), icStarView.getHeight()));
            iv.setPixelMap(ResourceTable.Media_star_floating);
            final FloatingElement floatingElement = new FloatingBuilder()
                    .anchorView(view)
                    .targetView(iv)
                    .offsetX(starOffsetX)
                    .floatingTransition(new StarFloating())
                    .build();
            SpringHelper.createWithBouncinessAndSpeed(0f, 1f, bounciness, speed).reboundListener(new ReboundListener() {
                @Override
                public void onReboundUpdate(double currentValue) {
                    view.setScaleX((float) currentValue);
                    view.setScaleY((float) currentValue);
                }

                @Override
                public void onReboundEnd() {
                    mFloating.startFloating(floatingElement);
                }
            }).start();
        });
    }

    /**
     * 啤酒
     */
    private void setIcBeerView() {
        final Component icBeerView = findComponentById(ResourceTable.Id_icBeer);
        icBeerView.setClickedListener(view -> {
            Image iv = new Image(this);
            iv.setLayoutConfig(new ComponentContainer.LayoutConfig(icBeerView.getWidth(), icBeerView.getHeight()));
            iv.setPixelMap(ResourceTable.Media_beer);

            FloatingElement floatingElement = new FloatingBuilder()
                    .anchorView(view)
                    .targetView(iv)
                    .floatingTransition(new BeerFloating())
                    .build();
            mFloating.startFloating(floatingElement);
        });
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
